'use strict';

const os = require('os');
const moment = require('moment');
const async = require('async');

const Controller = require('../../../core/common');

class ApplyController extends Controller {

  async index() {
    const { ctx, app } = this;

    // this.ctx.validate({
    //   status: { type: 'number' },
    //   content: { type: 'string' },
    // },ctx.request.query);

    const { nickname, phone, type, status } = ctx.request.query;

    const where = {};
    type && Object.assign(where, { type });
    status && Object.assign(where, { status });
    nickname && Object.assign(where, { nickname: { '$like': `%${nickname}%` } });
    phone && Object.assign(where, { phone });

    // const aaa = ctx.app.jwt.sign({
    //   data: {
    //     _id: 1
    //   },
    //   exp: Math.floor(Date.now() / 1000) + (60 * 60 * 24 * 7)
    // }, ctx.app.config.jwt.secret)
    // const hash = await ctx.genHash('xiazhi123456');
    // console.log(hash)
    // console.log(ctx.compare(1, 1), 'aaaaaaaaa')
    // const { startedAt } = ctx.request.query;
    // const aaa = moment(String(startedAt)).unix();
    // const bbb = moment(String(endAt)).unix();
    // console.log(aaa, bbb, bbb - aaa, 123)
    // await app.redis.get('expired').set(`goods_endAt_id_10`, 'data.id');
    // await app.redis.get('expired').expire(`goods_endAt_id_10`, bbb - aaa);
    // console.log(String(startedAt), moment(String(startedAt)).unix(), (Date.now() / 1000), 111)
    // return this.success(123)

    // const id = 1;
    // await app.redis.get('expired').set(`banner_endAt_id_${id}`, '');
    // await app.redis.get('expired').expire(`banner_endAt_id_${id}`, 5);

    const data = await ctx.service.v1.apply.findAll({ where });

    this.success(data);
  }

  async show() {
    const { ctx } = this;

    this.success('data');
  }

  async update() {
    const { ctx } = this;

    const { id = '' } = ctx.params;

    const { remark = '' } = ctx.request.body;

    const params = {};
    remark && Object.assign(params, { remark, status: 1 });
    const data = await ctx.service.v1.apply.update(params, {
      where: {
        id: id.indexOf(',') === -1 ? id : { '$in': id.split(',') }
      }
    });

    this.success(data);
  }

  async create() {
    const { ctx } = this;

    const data = await ctx.service.v1.apply.create(ctx.request.body);

    this.success(data)
  }

  async destroy() {
    const { ctx } = this;

    const { id = '' } = ctx.params;

    const data = await ctx.service.v1.apply.destroy({
      where: {
        id: id.indexOf(',') === -1 ? id : { '$in': id.split(',') }
      }
    });

    this.success(data);
  }

}

module.exports = ApplyController;